/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.client;

/*
Tooltip component for GWT
Copyright (C) 2006 Alexei Sokolov http://gwt.components.googlepages.com/

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback;

public class TooltipHandler implements MouseOverHandler, MouseOutHandler {
  private static final String DEFAULT_TOOLTIP_STYLE = "TooltipPopup";
  private static final int DEFAULT_OFFSET_X = 10;
  private static final int DEFAULT_OFFSET_Y = 0;

  private static class Tooltip extends PopupPanel {
    private int delay;

    public Tooltip(Widget sender, int offsetX, int offsetY, 
        final String text, final int delay, final String styleName) {
      super(true);

      this.delay = delay;

      HTML contents = new HTML(text);
      add(contents);

      setStyleName(styleName);
    }

    public void show() {
      super.show();

      Timer t = new Timer() {

        public void run() {
          Tooltip.this.hide();
        }

      };
      t.schedule(delay);
    }
  }

  private Tooltip tooltip;
  private String text;
  private String styleName;
  private int delay;
  private int offsetX = DEFAULT_OFFSET_X;
  private int offsetY = DEFAULT_OFFSET_Y;

  public TooltipHandler(String text, int delay) {
    this(text, delay, DEFAULT_TOOLTIP_STYLE);
  }

  public TooltipHandler(String text, int delay, String styleName) {
    this.text = text;
    this.delay = delay;
    this.styleName = styleName;
  }

  public void onMouseOver(MouseOverEvent event) {
	Widget sender=(Widget) event.getSource();
    if (tooltip != null) {
      tooltip.hide();
    }
    tooltip = new Tooltip(sender, offsetX, offsetY, text, delay, styleName);
    int left = sender.getAbsoluteLeft() + offsetX;
    int top = sender.getAbsoluteTop() + sender.getOffsetHeight() + offsetY;
    tooltip.setPopupPosition(left, top);
    tooltip.setPopupPositionAndShow(new PositionCallback(){
		
		@Override
		public void setPosition(int offsetWidth, int offsetHeight) {
		      int absoluteRight = tooltip.getAbsoluteLeft() + offsetWidth;
				int visibleRight = Window.getClientWidth();
				if (absoluteRight>=visibleRight) {
					int newLeft = Window.getClientWidth()-offsetWidth;
					tooltip.setPopupPosition(newLeft, tooltip.getAbsoluteTop());
				}
		}
		
	});
  }

  public void onMouseOut(MouseOutEvent event) {
    if (tooltip != null) {
      tooltip.hide();
    }
  }

  public String getStyleName() {
    return styleName;
  }

  public void setStyleName(String styleName) {
    this.styleName = styleName;
  }

  public int getOffsetX() {
    return offsetX;
  }

  public void setOffsetX(int offsetX) {
    this.offsetX = offsetX;
  }

  public int getOffsetY() {
    return offsetY;
  }

  public void setOffsetY(int offsetY) {
    this.offsetY = offsetY;
  }
} 